# This files contains your custom actions which can be used to run
# custom Python code.
#
# See this guide on how to implement these action:
# https://rasa.com/docs/rasa/custom-actions


# This is a simple example for a custom action which utters "Hello World!"

# from typing import Any, Text, Dict, List

# from rasa_sdk import Action, Tracker
# from rasa_sdk.executor import CollectingDispatcher


# class ActionHelloWorld(Action):

#     def name(self) -> Text:
#         return "action_hello_world"

#     def run(self, dispatcher: CollectingDispatcher,
#             tracker: Tracker,
#             domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:

#         dispatcher.utter_message(text="Hello World!")

#         return []

from typing import Any, Text, Dict, List

from rasa_sdk import Action, Tracker
from rasa_sdk.executor import CollectingDispatcher
import os, sys
sys.path.append(os.getcwd())
from actions.load_knowledge import Introduction, PeopleInform, RoomInform


INTRO = Introduction()
PEOPLE = PeopleInform()
ROOM = RoomInform()
floor_map = {'1': 'first', '2': 'second', '3': 'third'}
current_floor = '1'

# 介绍
class ActionIntroduceObject(Action):

    def name(self) -> Text:
        return "action_introduce_object"

    def run(self, dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:

        intro_obj = tracker.get_slot("introduce_object")
        if not intro_obj:
            dispatcher.utter_message(response="utter_ask_objintroduce")
        if intro_obj != 'laboratory':
            dispatcher.utter_message(text="Sorry, i can only introduce TJARK laboratory now!")
        else:
            name = intro_obj + " TJARK."
            reply = ' '.join([name, INTRO[intro_obj]['information'], INTRO[intro_obj]['remark']])
            dispatcher.utter_message(text=reply)

        return []

# 找人
class ActionFindSomeone(Action):

    def name(self) -> Text:
        return "action_find_someone"

    def run(self, dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:

        name = tracker.get_slot("person_to_visit")
        if not name:
            dispatcher.utter_message(response="utter_ask_personvisit")
        name = name[0].upper() + name[1:].lower()
        if name not in PEOPLE.keys():
            reply = text="Sorry, there is no such person, {}!".format(name)
        elif PEOPLE[name]['state'] == 'off work':
            reply = "{} is not here now. You could come later or call phone number {}.".format(name, PEOPLE[name]['tel_num'])
        else:
            reply = "The room number of {} is {}.".format(name, PEOPLE[name]['room'])
        
        dispatcher.utter_message(text=reply)

        return []

# 找房间
class ActionRoomGuide(Action):

    def name(self) -> Text:
        return "action_search_room"

    def run(self, dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:

        room = tracker.get_slot("room")
        room_num = ''.join(list(filter(str.isdigit, room)))
        if not room:
            dispatcher.utter_message(response="utter_ask_place")
        if room == 'washroom':
            room_num = '{}01'.format(current_floor)
            reply = 'The washroom is {}'.format(ROOM[room_num]['detail'])
        elif not room_num.isdigit():
            reply = "Wrong room number {}.".format(room_num)
        elif room_num not in ROOM.keys():
            reply = "There is no room {}.".format(room_num)
        else:
            reply = "Room {} is a/an {}, on the {} floor. {}".format(room_num, ROOM[room_num]['room_type'], 
                        floor_map[ROOM[room_num]['floor']], ROOM[room_num]['detail'])

        dispatcher.utter_message(text=reply)

        return []

class ActionPunch(Action):

    def name(self) -> Text:
        return "action_punch"

    def run(self, dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:

        name = tracker.get_slot("person_punch")
        if not name:
            dispatcher.utter_message(response="utter_ask_personpunch")
        name = name[0].upper() + name[1:].lower()
        if name not in PEOPLE.keys():
            reply = text="Sorry, there is no such person, {}!".format(name)
        elif PEOPLE[name]['state'] == 'off work':
            PEOPLE[name]['state'] = 'on work'
            reply = "OK. {} has punched in.".format(name)
        elif PEOPLE[name]['state'] == 'on work':
            PEOPLE[name]['state'] = 'off work'
            reply = "OK. {} has gotten off work.".format(name)
        
        PEOPLE.update()
        dispatcher.utter_message(text=reply)


        return []
